import appconf from "@/common/appconf.js";
const domain = appconf.domain;
import zySegmented from "@/uni_modules/zy-segmented-control/components/zy-segmented-control/zy-segmented-control.vue";
export default {
	components: {
		zySegmented
	},
	data() {
		return {
			flag: 0,
			noneFlag: false,
			items: ['今日秒杀', '下期预告'], //分段器名称
			current: 1, //分段器默认初始值
			noMore: false, // 没有更多了
			Inv: 1,
			exchangeCode: '',
			validPackets: [],
			hasUsedPackets: [],
			expiredPackets: [],
			hasGetUsedList: false, // 是否请求过已使用表单
			hasGetexpiredList: false, // 是否请求过已过期表单
			validTotal: 0,
			usedTotal: 0,
			expiredTotal: 0,
			validPageNo: 1,
			validPageSize: 5,
			usedPageNo: 1,
			usedPageSize: 5,
			expiredPageNo: 1,
			expiredPageSize: 5
		}
	},

	onLoad() {
		const onLogin = () => {
			this.getRedPacket(this.Inv); // 获取订单
		}
		this.$util.checkLogin(this, onLogin);
	},
	onReachBottom() {
		console.log('下滑')
		console.log(this.Inv, 'inv')
		console.log(this.validPackets, '--', this.validTotal)
		switch (this.Inv) {
			case 1:
				if (this.validPackets.length < this.validTotal) {
					this.validPageNo++;
					this.getRedPacket(1)
				}
				break;
			case 2:
				this.usedPageNo++;
				if (this.hasUsedPackets.length < this.usedTotal) {
					this.validPageNo++;
					this.getRedPacket(2)
				}
				break;
			case 3:
				this.expiredPageNo++;
				if (this.expiredPackets.length < this.expiredTotal) {
					this.validPageNo++;
					this.getRedPacket(3)
				}
				break;
			default:
				break;
		}

	},
	methods: {
		JumpActive() {
			uni.navigateTo({
				url: "/pages/index/active/active"
			})
		},
		back() {
			uni.navigateBack({
				delta: 1
			})
		},
		// 分段器选项发生改变时触发
		onClickItem(e) {
			this.current = e
			console.log('e:', e)
			this.flag = e


		},
		changeTab(Inv) {
			this.Inv = Inv;
			switch (Inv) {
				case 1:
					break;
				case 2:
					!this.hasGetUsedList && this.getRedPacket(Inv);
					break;
				case 3:
					!this.hasGetexpiredList && this.getRedPacket(Inv);
					break;
				default:
					break;
			}
			this.$forceUpdate()
		},
		exchange() {
			const code = this.exchangeCode
			let userId = this.$store.state.userInfo.userId
			if (!code) {
				uni.showToast({
					title: '请输入兑换码',
					icon: "none"
				});
				return false
			}
			const data = {
				code: code,
				userId: userId
			}
			this.$request(this.$api.ExchangeRedPacket, data, 'get').then(res => {
				if (res.code === 200) {
					console.log(res)
					uni.showToast({
						title: '兑换成功',
						icon: "none"
					});
				} else {
					uni.showToast({
						title: res.message,
						icon: "none"
					});
				}
			})
		},
		changeEllipsis(item) {
			item.ellipsis = !item.ellipsis
		},
		getRedPacket(Inv) {
			let pageNo, pageSize
			switch (Inv) {
				case 1:
					pageNo = this.validPageNo
					pageSize = this.validPageSize
					break;
				case 2:
					this.hasGetUsedList = true;
					pageNo = this.usedPageNo
					pageSize = this.usedPageSize
					break;
				case 3:
					this.hasGetexpiredList = true;
					pageNo = this.expiredPageNo
					pageSize = this.expiredPageSize
					break;
				default:
					break;

			}
			let userId = this.$store.state.userInfo.userId
			let data = {
				pageNo,
				pageSize,
				status: Inv,
				userId
			}
			this.$request(this.$api.getRedPacket, data, 'get').then(res => {
				if (res.code === 200) {
					let list
					switch (Inv) {
						case 1:
							list = this.validPackets;
							this.validTotal = Number(res.result.total)
							break;
						case 2:
							list = this.hasUsedPackets;
							this.usedTotal = Number(res.result.total)
							break;
						case 3:
							list = this.expiredPackets;
							this.expiredTotal = Number(res.result.total)
							break;
					}
					res.result.records.forEach(item => {
						item.ellipsis = true
						item.deviceIdStr = ""
						for (let i = 0; i < item.deviceList.length; i++) {
							if (item.deviceIdStr == "") {
								item.deviceIdStr += item.deviceList[i]['deviceId']
								console.log(item.deviceIdStr)
							} else {
								item.deviceIdStr += "、"
								item.deviceIdStr += item.deviceList[i]['deviceId']
							}
						}
						list.push(item)
					})

				}
			})
		}
	}
}